﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.MucHocBongService;
namespace eTMS.Controllers{
public class MucHocBongControllerBase : CommonController{
	public MucHocBongControllerBase()
		: base()
	{	
		this.dao = new MucHocBongServiceClient();
	}
	~MucHocBongControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private MucHocBongEntity oMucHocBong;	
	public MucHocBongEntity MucHocBongObject{
		get{
			return oMucHocBong;
		}
		set{
			oMucHocBong=value;
		}
	}
	protected MucHocBongServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSMucHocBong(false);			
			return _Table;
		}
	}
	public void LoadAllDSMucHocBong(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oMucHocBong==null)return false;
		bool result=true;
		try{
			oMucHocBong = dao.SaveRet(oMucHocBong);
		}
		catch{
			result=false;
		}
		if(result){
			_ListMucHocBong.Add(oMucHocBong);
		}
		return result;
	}
	public bool Update(){
		if(oMucHocBong==null)return false;
		return dao.Update(oMucHocBong);
	}
	public bool Delete(){
		if(oMucHocBong==null)return false;
		bool retvalue = dao.Delete(oMucHocBong);
		if(retvalue){			
			_ListMucHocBong.Remove(oMucHocBong);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 MucHocBong_ID){
		bool retvalue = dao.DeleteByID(MucHocBong_ID);		
		if(retvalue){
			MucHocBongEntity oDeleted=FindMucHocBong(MucHocBong_ID);
			if(oDeleted!=null){
				_ListMucHocBong.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oMucHocBong = new MucHocBongEntity();
	}
    //Copy data from data row in grid to object
    public MucHocBongEntity MapDataRowToObject(DataRow row){
		MucHocBongEntity o = new MucHocBongEntity();
		 if(!row.IsNull("TuDiem"))
			o.TuDiem=ConvertNumber(row["TuDiem"]);
		 if(!row.IsNull("DenDiem"))
			o.DenDiem=ConvertNumber(row["DenDiem"]);
		 if(!row.IsNull("SoTien"))
			o.SoTien=ConvertNumber(row["SoTien"]);
		 if(!row.IsNull("LayCanDuoi"))
			o.LayCanDuoi=ConvertBool(row["LayCanDuoi"]);
		 if(!row.IsNull("LayCanTren"))
			o.LayCanTren=ConvertBool(row["LayCanTren"]);
		 o.TenMuc=Convert.ToString(row["TenMuc"]);
		 if(!row.IsNull("CachTinhHocBong_ID"))
			o.CachTinhHocBong_ID=(Int32)(row["CachTinhHocBong_ID"]);
		 if(!row.IsNull("MucHocBong_ID"))
		    o.MucHocBong_ID=(Int32)(row["MucHocBong_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(MucHocBongEntity o,DataRow row){
		row["TuDiem"]=o.TuDiem;
		row["DenDiem"]=o.DenDiem;
		row["SoTien"]=o.SoTien;
		row["LayCanDuoi"]=o.LayCanDuoi;
		row["LayCanTren"]=o.LayCanTren;
		row["TenMuc"]=o.TenMuc;
		row["CachTinhHocBong_ID"]=o.CachTinhHocBong_ID;
		row["MucHocBong_ID"]=o.MucHocBong_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.MucHocBongObject,row);
    }
	public void LoadObject(Int32 MucHocBong_ID){
		MucHocBongEntity oLoad=FindMucHocBong(MucHocBong_ID);
		if(oLoad==null){
			oMucHocBong=this.LoadMucHocBong(MucHocBong_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oMucHocBong.IsNullMucHocBong_ID;
			if(!bNotIsNull){
				this.ListMucHocBong.Add(oMucHocBong);
			}
		}
		else{
			oMucHocBong=oLoad;
		}
	}
	#region CachedList MucHocBong
	private static List<MucHocBongEntity> _ListMucHocBong=new List<MucHocBongEntity>();
    public List<MucHocBongEntity>ListMucHocBong{
		get{
			return _ListMucHocBong;
		}
	}
	public static List<MucHocBongEntity>ListMucHocBongStatic{
		get{
			return _ListMucHocBong;
		}
	}
	public MucHocBongEntity FindMucHocBong(Int32 MucHocBong_ID){
		MucHocBongEntity oResult=this.ListMucHocBong.Find(delegate(MucHocBongEntity oFind){
			bool result=true; 
			result&=oFind.MucHocBong_ID.Equals(MucHocBong_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllMucHocBong(bool clearCache){
		if(_ListMucHocBong.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		MucHocBongServiceClient dao = new MucHocBongServiceClient();
		_ListMucHocBong=new List<MucHocBongEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public MucHocBongEntity LoadMucHocBong(Int32 MucHocBong_ID){			
		MucHocBongEntity o=dao.SelectOne(MucHocBong_ID);		
		return o;
	}
	#endregion
}
}
